<template>
  <el-card class="box-card">
    <div class="model-role">
      <el-form :inline="true" :model="dataForm" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="dataForm.roleName" clearable placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item><el-button @click="getDateList">查询</el-button></el-form-item>
        <el-form-item><el-button type="primary" @click="openDialog()">添加角色</el-button></el-form-item>
      </el-form>
      <el-table :data="dataList" border style="width: 100%">
        <el-table-column prop="roleName" label="角色名称" width="300"></el-table-column>
        <el-table-column prop="description" label="角色描述" width="300"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[5,10,20]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total,sizes,prev,pager,next,jumper"
        style="margin-top: 30px">
      </el-pagination>
    </div>

    <el-dialog :title="dataDialogForm.id === '' ?  dialogTitle:dialogTitle='更新角色信息' " :visible.sync="dialogFormVisible" @close="closeDialog()">
      <el-form :model="dataDialogForm" :rules="rules" ref="ruleForm">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="dataDialogForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" :label-width="formLabelWidth" prop="role_name">
          <el-input v-model="dataDialogForm.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="handleConfirm()">确 定</el-button>
      </div>
    </el-dialog>

  </el-card>
</template>

<script>

export default {
  name: 'customer',
  data() {
    return {
      dialogTitle: '添加角色',
      dataForm: {
        roleName: "",
      },
      ruleForm: {
        pass: '',
      },
      dataDialogForm: {
        id:"",
        roleName:"",
        description:""

      },
      dataList: [],
      userName: "xiaohei",
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      datalistLoading: false,
      dialogFormVisible: false,
      dialogFormSubmitVisible: false,
      formLabelWidth: '120px',
    };
  },
  methods: {
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDateList()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDateList()
    },
    getDateList() {
      // this.datalistLoading = true
      //生命params 分页查询的参数
      const params = {params: {pageIndex: this.pageIndex, pageSize: this.pageSize}}
      this.$http.get('/role/findByPage', params).then((res) => {
        console.log(res)
        this.dataList = res.data.data.records
        this.totalPage = res.data.data.current
      })
    },
    handleEdit(index, item) {
      //打开更新窗口
      this.dialogFormVisible = true
      this.dataDialogForm.id = item.id
      this.dataDialogForm.roleName = item.roleName
      this.dataDialogForm.description = item.description
      console.log('item', item)
    },
    handleDelete(index, item) {
      this.$confirm('此操作将永久删除该区域信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete('/role/' + item.id).then((res) => {
          console.log(item.id)
          this.getDateList()
          this.closeDialog()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //添加用户按钮绑定的方法
    openDialog() {
      //打开窗口
      this.dialogFormVisible = true
    },
    closeDialog() {
      //关闭窗口
      //清空表单
      this.dialogFormVisible = false
      this.dialogTitle = '添加角色';
      this.dataDialogForm.id=''
      this.dataDialogForm.roleName = ''
      this.dataDialogForm.description = ''
    },
    handleConfirm() {
      // 当点击确定按钮时，访问当前对话框的标题
      const title = this.dialogTitle;
      console.log('当前对话框标题:', title);

      // 根据标题执行不同的逻辑
      if (title === '添加角色') {
        this.handleSubmitFormData('ruleForm');
      } else if (title === '更新角色信息') {
        this.handleUpdateFormData('ruleForm');
      }

      // 可以在这里添加其他需要的逻辑
    },
    handleSubmitFormData(ruleForm) {
      //打开窗口
      // this.dialogFormVisible = true
      this.addRole(ruleForm)
    },
    handleUpdateFormData(ruleForm) {
      this.updateRegion(ruleForm)
    },
    updateRegion(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        this.$http.put('/role', this.dataDialogForm).then((res) => {
          console.log(res)
          this.dialogFormVisible = false   //关闭窗口
          this.closeDialog()
          this.getDateList()
        })
      });
    },
    addRole(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
            this.$http.post('/role', this.dataDialogForm).then((res) => {
              console.log(res)
              this.dialogFormVisible = false   //关闭窗口
              this.closeDialog()
              this.dialogFormVisible = false
              this.dialogFormSubmitVisible = false
              this.getDateList()
            })
        } else {
          return false;
        }
      });
      
    }
  },
  mounted() {
    this.getDateList()
  },
}

</script>

